<template>
  <!-- 定额执行 -->
  <div class="chart-box2">
    <div class="chart-title">定额执行</div>
    <div class="chart-content">
      <div style="height: 30%" class="legend">
        <a-row class="h-100p" align="middle" :gutter="8">
          <a-col :span="6" class="text-number-1">
            <div>本年累计用水</div>
            <div class="number" :style="{ color: colors[0] }">
              <count-to
                :start-val="numTotal.water_total.oldVal"
                :end-val="numTotal.water_total.newVal"
                :duration="3000"
              />
              <span class="unit">㎡</span>
            </div>
          </a-col>
          <a-col :span="6" class="text-number-1">
            <div>用水定额额度</div>
            <div class="number" :style="{ color: colors[0] }">
              <count-to
                :start-val="numTotal.water_quota.oldVal"
                :end-val="numTotal.water_quota.newVal"
                :duration="3000"
              />
              <span class="unit">㎡</span>
            </div>
          </a-col>
          <a-col :span="6" class="text-number-1">
            <div>本年累计用电</div>
            <div class="number" :style="{ color: colors[1] }">
              <count-to
                :start-val="numTotal.el_total.oldVal"
                :end-val="numTotal.el_total.newVal"
                :duration="3000"
              />
              <span class="unit">kw·h</span>
            </div>
          </a-col>
          <a-col :span="6" class="text-number-1">
            <div>用电定额额度</div>
            <div class="number" :style="{ color: colors[1] }">
              <count-to
                :start-val="numTotal.el_quota.oldVal"
                :end-val="numTotal.el_quota.newVal"
                :duration="3000"
              />
              <span class="unit">kw·h</span>
            </div>
          </a-col>
        </a-row>
      </div>
      <div style="height: 10%">
        <Decoration10 class="chart-container" :color="['#17a3ac']" />
      </div>
      <div style="height: 60%">
        <div class="chart-container" :id="chartId"></div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  defineComponent,
  onMounted,
  reactive,
  toRefs,
  onBeforeUnmount,
  nextTick,
  watch,
  toRaw,
} from "vue";
import { Chart, registerTheme } from "@antv/g2";
import common from "../../utils/common";
import CountTo from "../countTo/index.vue";
import { Decoration10 } from "@kjgl77/datav-vue3";
import store from "../../../../store";

export default defineComponent({
  name: "QuotaExecute",
  components: { CountTo, Decoration10 },
  setup() {
    const state = reactive({
      chartId: "quotaExecuteChart",
      chart: null,
      colors: ["#1699b7", "#dc6a1f"],
      data: [],
      numTotal: {
        water_total: { ...common.countToInit },
        water_quota: { ...common.countToInit },
        el_total: { ...common.countToInit },
        el_quota: { ...common.countToInit },
      },
    });

    const initChart = () => {
      const data = state.data;

      state.chart = new Chart({
        container: state.chartId,
        autoFit: true,
        theme: "chartTheme",
        padding: [20, 0, 0, 60],
      });
      state.chart.data(data);
      state.chart.tooltip(false);

      state.chart.axis("value", false);
      state.chart.coordinate().transpose();
      state.chart.facet("mirror", {
        fields: ["type"],
        transpose: true,
        title: {
          style: {
            fill: common.theme.lightWhite,
          },
        },
        // showTitle: false,
        eachView: (view, facet) => {
          const facetIndex = facet.columnIndex;
          if (facetIndex === 0) {
            view.axis("title", {
              position: "top",
              label: {
                style: {
                  fill: common.theme.lightWhite,
                  fontSize: 12,
                },
              },
              tickLine: {
                alignTick: false,
                length: 0,
              },
              line: null,
            });
          } else {
            view.axis("title", false);
          }
          const color = state.colors[facetIndex];
          view
            .interval()
            .position("title*value")
            .color(color)
            .size(36)
            .label("value", (val) => {
              return {
                offset: -10,
                style: {
                  fill: "rgba(255,255,255,0.8)",
                  fontSize: 16,
                  stroke: null,
                  shadowBlur: 2,
                  textAlign: facetIndex === 1 ? "end" : "start",
                },
              };
            });
        },
      });
      state.chart.render();
    };

    watch(
      () => store.getters.centerControl.Energy_quotaImplement,
      (val) => {
        if (val) {
          getData();
        }
      }
    );

    const getData = () => {
      const { Energy_quotaImplement } = store.getters.centerControl;
      if (Energy_quotaImplement) {
        state.data = Energy_quotaImplement.list;
        state.data.forEach((item) => {
          state.numTotal[item.key] = {
            oldVal: state.numTotal[item.key].newVal,
            newVal: item.value,
          };
        });

        if (state.chart) {
          state.chart.changeData(toRaw(state.data));
        } else {
          nextTick(() => {
            initChart();
          });
        }
      }
    };

    onMounted(() => {
      registerTheme("chartTheme", common.chartTheme);
      getData();
    });

    onBeforeUnmount(() => {
      const chartBox = document.getElementById(state.chartId);
      if (chartBox) {
        chartBox.innerHTML = "";
      }
    });
    return {
      ...toRefs(state),
    };
  },
});
</script>

<style lang="less" scoped>
@import "../../styles/common.less";
.chart-content {
  display: flex;
  flex-direction: column;
  .text-number-1 {
    text-align: center;
    .number {
      margin-top: 0rem;
      font-size: 2.4rem;
      .unit {
        font-size: 1.2rem;
        margin-left: 0.5rem;
      }
    }
  }
}
</style>
